"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : similar to
Case Name   : similar to在使用escape时，逃逸字符为汉字可成功
Create At   : 2023.05.22
Owner       : @li-xin12345
Description :
    1、创建数据库，指定数据库使用的字符编码'utf8'
    2、使用步骤1创建的数据库，similar to使用escape，逃逸字符为汉字
    3、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
History     :
"""

import os
import unittest

from yat.test import Node

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class ViewTest(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.user_node = Node('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = 'db_similar_0034'
        self.encode_name = 'utf8'

    def test_view(self):
        text = '-----step1:创建数据库，指定数据库字符编码utf8;expect:成功-----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} " \
                  f"encoding = '{self.encode_name}';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd, dbname='postgres')
        self.log.info(sql_res)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, sql_res,
                      '执行失败' + text)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res,
                      '执行失败' + text)

        self.log.info('-----确认默认编码为utf8-----')
        res = self.sh_primary.execut_db_sql('show server_encoding;',
                                            dbname=f'{self.db_name}')
        self.log.info(res)
        self.assertEqual(self.encode_name,
                         res.splitlines()[-2].strip().lower(), text + '失败')

        text = '-----step2:使用步骤1创建的数据库，similar to使用escape，' \
               '逃逸字符为汉字; expect:成功-----'
        self.log.info(text)
        sql_cmd = '''SELECT 'abc' SIMILAR TO '我%(b|d)%' escape '我' AS RESULT;
        SELECT 'abc' like '我%b%' escape '我' AS RESULT;
        SELECT 'abc' ilike '我%b%' escape '我' AS RESULT;
        SELECT 'abc' not like '我%b%' escape '我' AS RESULT;
        '''
        self.log.info(sql_cmd)
        res = self.sh_primary.execut_db_sql(sql_cmd, dbname=f'{self.db_name}')
        self.assertNotIn('ERROR', res, text + '执行失败')

    def tearDown(self):
        self.log.info('-----step3:清理环境; expect:成功-----')
        drop_cmd = f"drop database if exists {self.db_name};"
        self.log.info(drop_cmd)
        drop_res = self.sh_primary.execut_db_sql(drop_cmd, dbname='postgres')
        self.log.info(drop_res)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_res, '删库失败')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')